package com.theo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.theo.entity.UserInfo;
import com.theo.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 方涛
 * @since 2021-02-02
 */
@Controller
@RequestMapping("/user-info")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/add")
    public String userRegister(UserInfo user, Model model) {
        userInfoService.addUser(user);
            model.addAttribute("msg", "注册成功");
            return "user/login";
    }

    @RequestMapping("/add.do")
    public String addUser(UserInfo user) {
        userInfoService.addUser(user);
        return "redirect:/user-info/show.do";
    }
    @RequestMapping("/login")
    public String login(UserInfo user, Model model, HttpSession session) {
        UserInfo u = userInfoService.findByAccount(user);
        if (u != null && u.getPassword().equals(user.getPassword())) {
            session.setAttribute("loginUser", u);
            return "epidemic";
        } else {
            model.addAttribute("msg", "账号密码错误，请重试");
            return "redirect:/charts";
        }
    }

    @ResponseBody
    @RequestMapping("/validateLoginName")
    public String validateUser(@RequestParam("loginName") String loginName) {
        System.out.println(loginName);
        return userInfoService.validateLoginName(loginName);
    }

    @RequestMapping("/userLogout.do")
    public String logout(HttpSession session) {
        session.removeAttribute("loginUser");
        return "redirect:/charts";
    }

    @RequestMapping("/show.do")
    public String showUserList(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<UserInfo> users = userInfoService.queryUserList();
        users.forEach(System.out::println);
        PageInfo pageInfo = new PageInfo(users, 2);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/user_list";
    }

    @RequestMapping("/toUpdate.do")
    public String showUpdate(Model model,Integer id) {
        UserInfo user = userInfoService.queryUserById(id);
        model.addAttribute("user", user);
        return "admin/user_update";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Model model,Integer id) {
        UserInfo user = userInfoService.queryUserById(id);
        model.addAttribute("user", user);
        return "user/user_update";
    }

    @RequestMapping("/delete.do")
    public String deleteUser(@RequestParam("id") Integer id) {
        userInfoService.deleteUser(id);
        return "redirect:/user-info/show.do";
    }

    @RequestMapping("/update.do")
    public String update(UserInfo user) {
        userInfoService.updateUser(user);
        return "redirect:/user-info/show.do";
    }
    @RequestMapping("/update")
    public String updateUser(UserInfo user,HttpSession session,Model model) {
        userInfoService.updateUser(user);
        UserInfo u = (UserInfo) session.getAttribute("loginUser");
        int role = u.getRole();
        if (role != 1) {
            model.addAttribute("message", "信息变更成功，请重新登录");
            return "user/login";
        } else {
            return "redirect:/user-info/show.do";
        }
    }
}

